package com.算法.操作系统;

import javax.swing.*;

/**
 * @author zh
 * @date 2024-01-01 16:01
 */
public class Scheduling_Algorithm {

    public void TRR(Queue readyQueue, Queue blockedQueue, int timeSlice, JTextArea textArea) {
        int processNums = readyQueue.size();
        int count = 0;
        String str = "";
        textArea.append(" |============================================================================================================|\n");
        while (processNums > 0) {
            if (readyQueue.isEmpty()) {
                continue;
            }
            PCB process = readyQueue.dequeue();
            int randomNumber = 0;
            if (process.getRemainingTime() > timeSlice) {
                process.setRemainingTime(process.getRemainingTime() - timeSlice);
                process.setUsedCPUTime(process.getUsedCPUTime() + timeSlice);
                count += timeSlice;
                updateTextArea(" | 进程 [ " + process.getName() + " ] 当前时间片使用完, 更新周转时间, 进入就绪队列尾部 \n",textArea);
                updateTextArea(" |============================================================================================================|\n",textArea);
                blockedQueue.enqueue(process);
                try {
                    Thread.sleep(100); // 模拟堵塞时间
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                PCB process1 = blockedQueue.dequeue();
                readyQueue.enqueue(process1);
            } else {
                process.setUsedCPUTime(process.getUsedCPUTime() + process.getRemainingTime());
                if (count == 0) {
                    count += process.getRemainingTime();
                    process.setRevolveTime(process.getRemainingTime() + process.getIOTime() - process.getArrivalTime());
                } else {
                    count += process.getRemainingTime();
                    process.setRevolveTime(count + process.getIOTime() - process.getArrivalTime());
                }
                System.out.println("当前进程 " + process.getName() + " 执行完成, CPU 使用时间: " + process.getUsedCPUTime() + " ms, 周转时间: " + process.getRevolveTime() + " ms, 当前进程状态: 执行完成！");
                double wait = (double)process.getRevolveTime() - (double)process.getRemainingTime();
                double response = (wait / (double)process.getUsedCPUTime()) + 1;
                updateTextArea(" | 进程 [ " + process.getName() + " ] 执行完成,CPU使用时间: [ " + process.getUsedCPUTime() + " ms ],周转时间: [ " + process.getRevolveTime() + " ms ],响应比为: [" + response + "]\n",textArea);
                updateTextArea(" |============================================================================================================|\n",textArea);
                str += process.getName() + "-->";
                processNums--;
            }
        }
        int lastIndex = str.lastIndexOf("-->");
        if (lastIndex != -1) {
            String extractedString = str.substring(0, lastIndex);
            updateTextArea("\n TRR算法 结束顺序为：" + extractedString + "\n", textArea);
        } else {
            updateTextArea("\n TRR算法 结束顺序为：" + str + "\n", textArea);
        }
    }
    // 短作业优先
    public void SPF(Queue readyQueue, Queue blockedQueue, JTextArea textArea) {
        int count = 0;
        String str = "";
        int processNums = readyQueue.size();
        updateTextArea(" |============================================================================================================|\n",textArea);
        while (processNums > 0) {
            readyQueue.sortBySizeDescending(); // 按执行时间从小到大排序
            if (readyQueue.isEmpty()) {
                continue;
            }
            PCB process = readyQueue.dequeue();
            process.setRevolveTime(count + process.getRemainingTime() + process.getIOTime() - process.getArrivalTime());
            count += process.getRemainingTime();
            process.setUsedCPUTime(process.getRemainingTime());
            double wait = (double)process.getRevolveTime() - (double)process.getRemainingTime();
            double response = (wait / (double)process.getUsedCPUTime()) + 1;
            updateTextArea(" | 进程 [ " + process.getName() + " ] 执行完成,CPU使用时间: [ " + process.getUsedCPUTime() + " ms ],周转时间: [ " + process.getRevolveTime() + " ms ],响应比为: [ " + response + " ]\n",textArea);
            updateTextArea(" |============================================================================================================|\n",textArea);
            str += process.getName() + "-->";
            processNums--;
        }

        int lastIndex = str.lastIndexOf("-->");
        if (lastIndex != -1) {
            String extractedString = str.substring(0, lastIndex);
            updateTextArea(" SPF算法 结束顺序为："+extractedString + "\n", textArea);
        } else {
            updateTextArea(" SPF算法 结束顺序为："+lastIndex + "\n", textArea);
        }
    }
    // 更新文本域
    private void updateTextArea(String content, JTextArea textArea) {
        textArea.append(content);
    }
}
